#pragma once
#include "protocol.hpp"
enum
{
    Success = 0,
    DivZeroErr,
    ModZeroErr,
    UnknownOper
};
// 计算器业务
namespace Calculate
{

    class Calculate
    {
    public:
        std::shared_ptr<Protocol::Response> Cal(std::shared_ptr<Protocol::Request> request)
        {
            std::shared_ptr<Protocol::Response> response = _factory.BuildResponse();
            // 默认设置为成功
            response->SetCode(Success);
            switch (request->GetOper())
            {
            case '+':
                response->SetResult(request->GetX() + request->GetY());
                break;
            case '-':
                response->SetResult(request->GetX() - request->GetY());
                break;
            case '*':
                response->SetResult(request->GetX() * request->GetY());
                break;
            case '/':
            {
                if (request->GetY() == 0)
                {
                    response->SetCode(DivZeroErr);
                }
                else
                {
                    response->SetResult(request->GetX() / request->GetY());
                }
            }
            break;
            case '%':
            {
                if (request->GetY() == 0)
                {
                    response->SetCode(ModZeroErr);
                }
                else
                {
                    response->SetResult(request->GetX() % request->GetY());
                }
            }
            break;
            default:
                response->SetCode(UnknownOper);
                break;
            }
            return response;
        }

    private:
        Protocol::Factory _factory;
    };
}